#!/bin/bash
#these were originally in the retired script /sbin/pup_event_frontend_d
#130610 bugfix remove desktop icon if label different from drv name.
#130613 npierce: free_coords() supports screen widths not multiple of 32.
#130807 L18L: set executable in Woof 'VCSMETEDATA' file. move gettext declaration from other scripts.

pidof ROX-Filer &>/dev/null && ROX_DESKTOP=yes

. /etc/rc.d/functions_x #fx_* functions

#130807 L18L
export TEXTDOMAIN=pup_event_frontend_d
export OUTPUT_CHARSET=UTF-8
. gettext.sh

#------------------------------------------------------------
#v403 change desktop icon to unmounted status, called from pup_event_frontend_d, pmount...
icon_unmounted_func() {
 [ "$ROX_DESKTOP" != "yes" ] && return
 #passed params: drivename category  ex: sda usbdrv
 DRVICON="drive48.png" #default.
 [ "$2" = "usbdrv" ] && DRVICON="usbdrv48.png"
 [ "$2" = "card" ] && DRVICON="card48.png"
 [ "$2" = "floppy" ] && DRVICON="floppy48.png"
 [ "$2" = "optical" ] && DRVICON="optical48.png"
 [ "$3" = "crypto_LUKS" ] && DRVICON="lock-screen48.png"
 #find out if already done...
 if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons ];then
  GLOBLINES="`cat /root/.config/rox.sourceforge.net/ROX-Filer/globicons | tr '\n' '|' | sed -e 's%</rule>|%\n%g'`"
  diPATTERN='/root/.pup_event/drive_'"${1}"'"'
  [ "`echo "$GLOBLINES" | grep "$diPATTERN" | grep "$DRVICON"`" != "" ] && return
 fi
 #[ "`echo "${ATADRIVES}" | grep "${1}"`" = "" ] && sleep 1 #110722 usb drive, LED light briefly flashes after unmounted, so wait a bit. ATADRIVES is in /etc/rc.d/PUPSTATE.
 echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <SetIcon>
   <Path>/root/.pup_event/drive_${1}</Path>
   <Icon>/usr/local/lib/X11/pixmaps/${DRVICON}</Icon>
  </SetIcon>
 </env:Body>
</env:Envelope>"  | roxfiler -R
}

icon_mounted_func() {
 [ "$ROX_DESKTOP" != "yes" ] && return
 #passed params: drivename category  ex: sda usbdrv
 DRVICON="drive_mntd48.png" #default.
 [ "$2" = "usbdrv" ] && DRVICON="usbdrv_mntd48.png"
 [ "$2" = "card" ] && DRVICON="card_mntd48.png"
 [ "$2" = "floppy" ] && DRVICON="floppy_mntd48.png"
 [ "$2" = "optical" ] && DRVICON="optical_mntd48.png"
 imPATTERN="^/dev/${1} |^/dev/mapper/lukspartition_${1} " #support luks-encrypted partitions
 if [ "`mount | grep -E "$imPATTERN" | cut -f 1,3 -d ' ' | grep -v ' /initrd/' | grep -v ' /$'`" = "" ];then
  #only partitions mntd on /initrd/* then must be mntd at boot. cannot unmount.
  #also, full hd install has partition mntd on '/'.
  DRVICON="drive_mntd_boot48.png" #default.
  [ "$2" = "usbdrv" ] && DRVICON="usbdrv_mntd_boot48.png"
  [ "$2" = "card" ] && DRVICON="card_mntd_boot48.png"
  [ "$2" = "floppy" ] && DRVICON="floppy_mntd_boot48.png"
  [ "$2" = "optical" ] && DRVICON="optical_mntd_boot48.png"
 fi
 #find out if already done...
 if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons ];then
  GLOBLINES="`cat /root/.config/rox.sourceforge.net/ROX-Filer/globicons | tr '\n' '|' | sed -e 's%</rule>|%\n%g'`"
  diPATTERN='/root/.pup_event/drive_'"${1}"'"'
  [ "`echo "$GLOBLINES" | grep "$diPATTERN" | grep "$DRVICON"`" != "" ] && return
 fi
 echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <SetIcon>
   <Path>/root/.pup_event/drive_${1}</Path>
   <Icon>/usr/local/lib/X11/pixmaps/${DRVICON}</Icon>
  </SetIcon>
 </env:Body>
</env:Envelope>"  | roxfiler -R
}
#------------------------------------------------------------

create_appinfo_func() { #needs INFO, DRV_CATEGORY, MOREITEMS, ONEDRVNAME
   echo "<?xml version=\"1.0\"?>
<AppInfo>
  <Summary>${INFO}</Summary>
  <About>
   <Purpose>`gettext \"Drive icons are part of Puppy Event Manager, see System menu\"`</Purpose>
   <Authors>Barry Kauler LGPL 2008</Authors>
  </About>
  <AppMenu>
   <Item option=\"pmount_${DRV_CATEGORY}\" icon=\"gtk-harddisk\"><Label>$(gettext 'Run Pmount Puppy Drive Mounter')</Label></Item><Item option=\"eventmanager\" icon=\"gtk-execute\"><Label>$(gettext 'Run Desktop Drive Icon Manager')</Label></Item>${MOREITEMS}
  </AppMenu>
</AppInfo>" > /root/.pup_event/drive_${ONEDRVNAME}/AppInfo.xml
}

free_coord() {
 #find a free place on desktop. v410 avoid exact overlapping icons...
 #091208 finetune placement, also any side of screen...
 #read the coords grid, code from shinobar...
 if [ -z "$SCRN_X" -o -z "$SCRN_Y" ] ; then
  while read a b c ; do
	case $a in -geometry)
		#1366x768+0+0
		SCRNXY=${b%%+*} #1366x768
		read SCRN_X SCRN_Y <<< "${SCRNXY//x/ }"
		break
		;;
	esac
  done <<< "$(LANG=C xwininfo -root)"
 fi
 #echo $SCRN_X $SCRN_Y #DEBUG
 COORDSGRID="`grep -o ' x="[0-9]*" y="[0-9]*" ' /root/Choices/ROX-Filer/PuppyPin | sed 's/[0-9]"/"/g'`"
 PIN_GRID_STEP=`grep "pinboard_grid_step" /root/.config/rox.sourceforge.net/ROX-Filer/Options | sed -e "s/ *<[^>]*>//g"`
 [ ! $PIN_GRID_STEP ] && PIN_GRID_STEP=16 #16=medium.
 [ $PIN_GRID_STEP -eq 0 ] && PIN_GRID_STEP=16 #precaution.
 PIN_GRID_STEP2=$(($PIN_GRID_STEP / 2))
 MAX_X=$(( $SCRN_X - 96 ))
 MAX_Y=$(( $SCRN_Y - 96 ))
 #echo "MAX_X: $MAX_X MAX_Y: $MAX_Y" #DEBUG
 case $ICON_PLACE_ORIENTATION in #see /etc/eventmanager
  bottom)
   COORD_Y=$(( $SCRN_Y - $ICON_PLACE_EDGE_GAP )) #default 64, see /etc/eventmanager
   COORD_Y=$(( ( $COORD_Y + $PIN_GRID_STEP2 ) / $PIN_GRID_STEP * $PIN_GRID_STEP ))
   COORD_X=$ICON_PLACE_START_GAP #default 32, see /etc/eventmanager
   COORD_X=$(( ( $COORD_X + $PIN_GRID_STEP2 ) / $PIN_GRID_STEP *  $PIN_GRID_STEP )) #130613
   xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" | x=\"$((COORD_X + 1))\" y=\"$((COORD_Y + 1))\" "
   #echo "$xPATTERN" #DEBUG
   xyCNT=0 #precaution.
   while [ 1 ];do
    xgPATTERN="`echo -n "$xPATTERN" | sed 's/[0-9]"/"/g'`"
    #echo "$xgPATTERN" #DEBUG
    [ "`echo "$COORDSGRID" | grep -E "$xgPATTERN"`" = "" ] && break
    COORD_X=$(( $COORD_X + $ICON_PLACE_SPACING ))
    if [ $COORD_X -gt $MAX_X ];then #start next line up.
     xyCNT=$(( $xyCNT + 1 ))
     [ $xyCNT -gt 1 ] && break
     COORD_Y=$(( $COORD_Y - $ICON_PLACE_SPACING ))
     COORD_X=$ICON_PLACE_START_GAP
    fi
    xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" | x=\"$((COORD_X + 1))\" y=\"$((COORD_Y + 1))\" "
    #echo "$xPATTERN" #DEBUG
   done
  ;;
  top)
   COORD_Y=$ICON_PLACE_EDGE_GAP
   COORD_Y=$(( ( $COORD_Y + $PIN_GRID_STEP2 ) / $PIN_GRID_STEP * $PIN_GRID_STEP ))
   COORD_X=$ICON_PLACE_START_GAP #default 32, see /etc/eventmanager
   COORD_X=$(( ( $COORD_X + $PIN_GRID_STEP2 ) / $PIN_GRID_STEP * $PIN_GRID_STEP )) #130613
   xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" | x=\"$((COORD_X + 1))\" y=\"$((COORD_Y + 1))\" "
   xyCNT=0 #precaution.
   while [ 1 ];do
    xgPATTERN="`echo -n "$xPATTERN" |  sed 's/[0-9]"/"/g'`"
    [ "`echo "$COORDSGRID" | grep -E "$xgPATTERN"`" = "" ] && break
    COORD_X=$(( $COORD_X + $ICON_PLACE_SPACING ))
    if [ $COORD_X -gt $MAX_X ];then #start next line down.
     xyCNT=$(( $xyCNT + 1 ))
     [ $xyCNT -gt 1 ] && break
     COORD_Y=$(( $COORD_Y + $ICON_PLACE_SPACING ))
     COORD_X=$ICON_PLACE_START_GAP
    fi
    xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" | x=\"$((COORD_X + 1))\" y=\"$((COORD_Y + 1))\" "
   done
  ;;
  left)
   COORD_X=$ICON_PLACE_EDGE_GAP #default 64, see /etc/eventmanager
   COORD_X=$(( ( $COORD_X + $PIN_GRID_STEP2 ) / $PIN_GRID_STEP * $PIN_GRID_STEP )) #130613
   COORD_Y=$ICON_PLACE_START_GAP #default 32, see /etc/eventmanager
   COORD_Y=$(( ( $COORD_Y + $PIN_GRID_STEP2 ) / $PIN_GRID_STEP *  $PIN_GRID_STEP ))
   xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" | x=\"$((COORD_X + 1))\" y=\"$((COORD_Y + 1))\" "
   xyCNT=0 #precaution.
   while [ 1 ];do
    xgPATTERN="`echo -n "$xPATTERN" |  sed 's/[0-9]"/"/g'`"
    [ "`echo "$COORDSGRID" | grep -E "$xgPATTERN"`" = "" ] && break
    COORD_Y=$(( $COORD_Y + $ICON_PLACE_SPACING ))
    if [ $COORD_Y -gt $MAX_Y ];then #start next row.
     xyCNT=$(( $xyCNT + 1 ))
     [ $xyCNT -gt 1 ] && break
     COORD_X=$(( $COORD_X + $ICON_PLACE_SPACING ))
     COORD_Y=$ICON_PLACE_START_GAP
    fi
    xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" | x=\"$((COORD_X + 1))\" y=\"$((COORD_Y + 1))\" "
   done
  ;;
  right)
   COORD_X=$(( $SCRN_X - $ICON_PLACE_EDGE_GAP )) #default 64, see /etc/eventmanager
   COORD_X=$(( ( $COORD_X + $PIN_GRID_STEP2 ) / $PIN_GRID_STEP * $PIN_GRID_STEP )) #130613
   COORD_Y=$ICON_PLACE_START_GAP #default 32, see /etc/eventmanager
   COORD_Y=$(( ( $COORD_Y + $PIN_GRID_STEP2 ) / $PIN_GRID_STEP * $PIN_GRID_STEP ))
   xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" | x=\"$((COORD_X + 1))\" y=\"$((COORD_Y + 1))\" "
   xyCNT=0 #precaution.
   while [ 1 ];do
    xgPATTERN="`echo -n "$xPATTERN" |  sed 's/[0-9]"/"/g'`"
    [ "`echo "$COORDSGRID" | grep -E "$xgPATTERN"`" = "" ] && break
    COORD_Y=$(( $COORD_Y + $ICON_PLACE_SPACING ))
    if [ $COORD_Y -gt $MAX_Y ];then #start next row back.
     xyCNT=$(( $xyCNT + 1 ))
     [ $xyCNT -gt 1 ] && break
     COORD_X=$(( $COORD_X - $ICON_PLACE_SPACING ))
     COORD_Y=$ICON_PLACE_START_GAP
    fi
    xPATTERN=" x=\"${COORD_X}\" y=\"${COORD_Y}\" | x=\"$((COORD_X + 1))\" y=\"$((COORD_Y + 1))\" "
   done
  ;;
 esac
 #echo "$ICON_PLACE_ORIENTATION : $COORD_X $COORD_Y" #DEBUG
}

add_pinboard_func() { #needs ONEDRVNAME, DRV_CATEGORY, FSTYPE
 [ "$ROX_DESKTOP" != "yes" ] && return
 #v411 rerwin: avoid icon getting duplicated if it is renamed...
 ppPATTERN="/root/.pup_event/drive_${ONEDRVNAME}<"
 grep -q "$ppPATTERN" /root/Choices/ROX-Filer/PuppyPin && return #precaution.
 free_coord
 #120503 the label "mmcblk0p1" (SD memory cards via mmc interface) is too long...
 cutONEDRVNAME=${ONEDRVNAME//cblk/} #mmcblk0p1 -> mm0p1
 if [ "$LABELPARTITIONS" = "true" ] ; then
  if [ "$DRV_LABEL" ] ; then
	[ "${#DRV_LABEL}" -gt 8 ] && DRV_LABEL="${DRV_LABEL:0:7}.."
	cutONEDRVNAME="${cutONEDRVNAME}
${DRV_LABEL}"
  fi
 fi
 echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <PinboardAdd>
   <Path>/root/.pup_event/drive_${ONEDRVNAME}</Path>
   <X>${COORD_X}</X>
   <Y>${COORD_Y}</Y>
   <Label>${cutONEDRVNAME}</Label>
   <Args>${DRV_CATEGORY} ${FSTYPE}</Args>
  </PinboardAdd>
 </env:Body>
</env:Envelope>"  | roxfiler -R
}

create_icon_func() {
 [ "$ICONDESK" = "false" ] && "$ICONPARTITIONS" = "false"
 #create desktop icon, and directory with handler script, icon and help...
 #requires global variables DRV_NAME, DRV_CATEGORY, DRV_DESCRIPTION.
 if [ "$HOTPLUGON" = "true" -a "$ICONPARTITIONS" = "true" ] ; then
  #if a passed param $1, then PROBEPART already known, inefficient to rerun here each time.
  #display an icon for each partition... w476 add ext4... 130216 add f2fs...
  if [ ! $1 ] ; then
    # testing a bad optical drive there's a timing issue
    # and a race condition between frontend_timeout and frontend_change
    PS="`busybox ps`"
    echo "$PS" | grep "zzFRONTEND_CHANGE ${DRV_NAME}" && return
    #-
    PROBEPART="`probepart -k -extra-info zzFRONTEND_CHANGE "${DRV_NAME}.*" | grep -E '\|f2fs\||\|ext4\||\|ntfs\||\|msdos\||\|vfat\||\|exfat\||\|ext2\||\|ext3\||\|iso9660\||\|udf\||\|audiocd\||\|xfs\||\|reiserfs\||\|crypto_LUKS\||\|none'`"
  fi
  DRVINFO="`echo "$PROBEPART" | grep "^/dev/${DRV_NAME}" | sort --version-sort --field-separator='|' --key=1,1`"
  if [ -z "$DRV_DESCRIPTION" ] ; then
    DRV_DESCRIPTION="`probedisk ${DRV_NAME} | cut -f 3 -d '|'`"
  fi
 #--
 elif [ "$HOTPLUGON" = "true" -a "$ICONDESK" = "true" ] ; then
  #if a passed param $1, then PROBEDISK already known
  if [ ! $1 ] ; then
    PROBEDISK="$(probedisk -plus-size $DRV_NAME)"
    IFS="|" read -r ONEDRV DRV_CATEGORY DRV_DESCRIPTION DRV_SIZE zz <<< "$PROBEDISK"
    DRV_NAME=${ONEDRV##*/}
  fi
  DRV_SIZE=$(($DRV_SIZE/2)) # kb = /proc/partitions
  DRVINFO="$DRV_NAME|0|$DRV_SIZE|$DRV_DESCRIPTION"
  #FSTYPE=0 - see /usr/local/bin/drive_all
 else
  #only show a single 'drives' icon on desktop...
  DRV_NAME='drives'
  DRV_CATEGORY='any' #see pmount.
  DRV_DESCRIPTION="all drives"
  DRVINFO="$DRV_NAME"'|0|0'
 fi
 #--

 while IFS="|" read ONEDRVNAME FSTYPE SIZE DRV_LABEL zz
 do
  ONEDRVNAME=${ONEDRVNAME##*/} #basename
  if [ ! -d /root/.pup_event/drive_${ONEDRVNAME} ];then
   mkdir /root/.pup_event/drive_${ONEDRVNAME}
   cp -af /usr/local/bin/drive_all /root/.pup_event/drive_${ONEDRVNAME}/AppRun
   if [ "$FSTYPE" = "0" -a "$SIZE" = "0" ];then
     INFO="$(gettext 'Puppy drive manager')"
   elif [ "$ICONPARTITIONS" = "true" ] ; then
     SIZE=$( fx_format_bytes $(($SIZE * 1024)) ) # formats SIZE for display
     INFO="$(gettext 'Filesystem'): ${FSTYPE} $(gettext 'Size'): ${SIZE}  [${DRV_DESCRIPTION}]" #*WARNING* parsed in clean_desk_icons !!!
   elif [ "$ICONDESK" = "true" ] ; then
     SIZE=$( fx_format_bytes $(($SIZE * 1024)) ) # formats SIZE for display
     INFO="$(gettext 'Size'): ${SIZE}  [${DRV_DESCRIPTION}]"
   fi
   #v424 add item to unmount all partitions...
   MOREITEMS="<Item option=\"unmount\" icon=\"gtk-harddisk\"><Label>$(eval_gettext 'Unmount ${ONEDRVNAME} [if currently mounted]')</Label></Item><Item option=\"unmountall\" icon=\"gtk-harddisk\"><Label>$(gettext 'Unmount ALL mounted partitions')</Label></Item>"
   create_appinfo_func #needs INFO, DRV_CATEGORY, MOREITEMS, ONEDRVNAME
  fi
  MNTSTATUS="$(mount -v | grep -E "^/dev/${ONEDRVNAME} |^/dev/mapper/lukspartition_${ONEDRVNAME} ")"
  if [ "$MNTSTATUS" = "" ];then
   icon_unmounted_func "$ONEDRVNAME" "$DRV_CATEGORY" "$FSTYPE"
  else
   icon_mounted_func "$ONEDRVNAME" "$DRV_CATEGORY" "$FSTYPE"
  fi
  add_pinboard_func #needs ONEDRVNAME, DRV_CATEGORY, FSTYPE.
 done <<EOF
$DRVINFO
EOF
 #-
 #always create a handler for entire drive, even if no icon displayed...
 if [ ! -d /root/.pup_event/drive_${DRV_NAME} ];then
  if [ -e /sys/block/${DRV_NAME} ];then
   mkdir /root/.pup_event/drive_${DRV_NAME}
   cp -af /usr/local/bin/drive_all /root/.pup_event/drive_${DRV_NAME}/AppRun
   if [ -e /sys/block/${DRV_NAME}/size ];then
     read -r SIZE < /sys/block/${DRV_NAME}/size
     SIZE=$(($SIZE/2)) #get KB.
   fi
   SIZE=$( fx_format_bytes $(($SIZE * 1024)) ) # formats SIZE for display
   INFO="$(gettext 'Description'): ${DRV_DESCRIPTION} $(gettext 'Size'): ${SIZE}"
   #v424 add item to unmount all mounted partitions...
   MOREITEMS="</Item><Item option=\"unmountall\" icon=\"gtk-harddisk\"><Label>$(gettext 'Unmount ALL mounted partitions')</Label></Item>"
   ONEDRVNAME="$DRV_NAME"
   create_appinfo_func #needs INFO, DRV_CATEGORY, MOREITEMS, ONEDRVNAME
  fi
 fi
}

remove_pinboard_func() { #needs DRV_NAME (name of entire drive)
 [ "$ROX_DESKTOP" != "yes" ] && return
 DRV_NAME=$(fx_get_drvname $DRV_NAME)
 rpPTN="drive_${DRV_NAME}[^<]*" #130610 label may differ from drv name.
 [ "`grep "$rpPTN" /root/Choices/ROX-Filer/PuppyPin`" = "" ] && return
 ALLNAMES="`grep -o "$rpPTN" /root/Choices/ROX-Filer/PuppyPin | cut -f 2 -d '_' | cut -f 1 -d '<' | tr '\n' ' '`"
 for ONEDRVNAME in $ALLNAMES
 do
  echo "<?xml version=\"1.0\"?>
<env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">
 <env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">
  <PinboardRemove>
   <Path>/root/.pup_event/drive_${ONEDRVNAME}</Path>
  </PinboardRemove>
 </env:Body>
</env:Envelope>"  | roxfiler -R
 done
}


#==================================================================


rox_mount() {
 [ "$ROX_DESKTOP" != "yes" ] && return
 [ ! "$DISPLAY" ] && export DISPLAY=:0
 DEVNAME="`busybox mount | tail -n 1 | grep '^/dev/' | cut -f 1 -d ' ' | cut -f 3 -d '/'`"
 if [ "$DEVNAME" != "" ];then
  DRVNAME=$(fx_get_drvname $DEVNAME)
  xDRVNAME="$DRVNAME" #v404
  [ -d /root/.pup_event/drive_${DEVNAME} ] && DRVNAME="$DEVNAME" #icon for each partition.
  if [ -d /root/.pup_event/drive_${DRVNAME} ];then
    DRV_CATEGORY="`probedisk /dev/${xDRVNAME} | cut -f 2 -d '|'`"
    icon_mounted_func $DRVNAME $DRV_CATEGORY #see frontend_funcs
  fi
 fi
}

rox_umount() {
  [ "$ROX_DESKTOP" != "yes" ] && return
  [ ! "$DISPLAY" ] && export DISPLAY=:0
  DEVNAME="$1"
  DRVNAME=$(fx_get_drvname $DEVNAME)
  DRV_CATEGORY="`probedisk /dev/${DRVNAME} | cut -f 2 -d '|'`"
  [ -d /root/.pup_event/drive_${DEVNAME} ] && DRVNAME="$DEVNAME" #icon for each partition. ex: DRVNAME=sda1
  if [ -d /root/.pup_event/drive_${DRVNAME} ];then
   #if any partitions still mounted, leave "MNTD" on icon...
   drvPATTERN='^/dev/'"${DRVNAME} "
   #"MNTD" text is intended as a reminder to user that drive needs to be unmounted,
   #   but partitions in use by Puppy cannot be unmounted.
   if [ "`mount-FULL -v | tr -s ' ' | cut -f 1,3 -d ' ' | grep "$drvPATTERN" | grep -v ' /initrd/' | grep -v ' /$'`" = "" ];then
    if [ "`mount-FULL -v | tr -s ' ' | cut -f 1,3 -d ' ' | grep "$drvPATTERN" | grep -E ' /initrd/| /$'`" != "" ];then
     #only a partition left mntd that is in use by puppy, change green->yellow...
     icon_mounted_func $DRVNAME $DRV_CATEGORY #see frontend_funcs
    else
     #redraw icon without "MNTD" text...
     icon_unmounted_func $DRVNAME $DRV_CATEGORY #see frontend_funcs
    fi
   fi
  fi
}

rox_mount_luks() {
 [ "$ROX_DESKTOP" != "yes" ] && return
 [ ! "$DISPLAY" ] && export DISPLAY=:0
 DEVNAME="`busybox mount | tail -n 1 | grep '^/dev/' | cut -f 1 -d ' '`"
 DEVNAME=${DEVNAME##*/}            # basename $DEVNAME
 DEVNAME=${DEVNAME#lukspartition_} # remove lukspartition_ (ex: lukspartition_sdb1)
 if [ "$DEVNAME" != "" ];then
  DRVNAME=$(fx_get_drvname $DEVNAME)
  xDRVNAME="$DRVNAME" #v404
  [ -d /root/.pup_event/drive_${DEVNAME} ] && DRVNAME="$DEVNAME" #icon for each partition.
  if [ -d /root/.pup_event/drive_${DRVNAME} ];then
    DRV_CATEGORY="`probedisk "/dev/${xDRVNAME} " | cut -f 2 -d '|'`"
    icon_mounted_func $DRVNAME $DRV_CATEGORY crypto_LUKS #see frontend_funcs
  fi
 fi
}

rox_umount_luks() {
  [ "$ROX_DESKTOP" != "yes" ] && return
  [ ! "$DISPLAY" ] && export DISPLAY=:0
  DEVNAME="$1"
  DRVNAME=$(fx_get_drvname $DEVNAME)
  DRV_CATEGORY="`probedisk /dev/${DRVNAME} | cut -f 2 -d '|'`"
  [ -d /root/.pup_event/drive_${DEVNAME} ] && DRVNAME="$DEVNAME" #icon for each partition. ex: DRVNAME=sda1
  if [ -d /root/.pup_event/drive_${DRVNAME} ];then
   #if any partitions still mounted, leave "MNTD" on icon...
   drvPATTERN='^/dev/'"${DRVNAME} "
   #"MNTD" text is intended as a reminder to user that drive needs to be unmounted,
   #   but partitions in use by Puppy cannot be unmounted.
   if [ "`mount-FULL -v | tr -s ' ' | cut -f 1,3 -d ' ' | grep "$drvPATTERN" | grep -v ' /initrd/' | grep -v ' /$'`" = "" ];then
	if [ "`mount-FULL -v | tr -s ' ' | cut -f 1,3 -d ' ' | grep "$drvPATTERN" | grep -E ' /initrd/| /$'`" != "" ];then
	 #only a partition left mntd that is in use by puppy, change green->yellow...
	 icon_mounted_func $DRVNAME $DRV_CATEGORY crypto_LUKS #see frontend_funcs
	else
	 #redraw icon without "MNTD" text...
	 icon_unmounted_func $DRVNAME $DRV_CATEGORY crypto_LUKS #see frontend_funcs
	fi
   fi
  fi
}

if [ "$1" ] ; then
	$@
fi

### END ###